<?php
// +----------------------------------------------------------------------
// | 景区产品附属资料
// +----------------------------------------------------------------------
// | Author: TongBC <50249101@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/02/03
// +----------------------------------------------------------------------
include_once '/var/www/html/new/d/common/autoload.php';
include_once '/var/www/html/new/d/common/errBack.php';
include_once '/var/www/html/new/d/module/common/Db.class.php';


// 远端服务器配置信息
$dbConf = include '/var/www/html/new/d/module/common/db.conf.php';


use \PFT\Db;
Db::Conf($dbConf['remote_1']);


$s = SoapInit::GetSoapInside();
$apply_did = $_SESSION['sid'];


switch($_REQUEST['action']) {
    // 云游风景信息
	case 'scenic':

		$post = _addslashes($_POST);// 字符过滤
		$lid = $parent_id = $post['parent_id'];
        $scenic_name = $post['mainTitle'];
        $tour_guide = $post['detailInfo'];
		$province = $post['d_province'];
		$city = $post['d_city'];
		$daddress = $post['detail_address'];
		$thumb_path = $_POST['thumb_img'];
        $voice_path = $_POST['upfile_music_v'];
        $video_path = $_POST['upfile_video_v'];
		$degree_path = $_POST['upfile_allvideo'];

		$tmp = explode(',',$post['child_place_tude']);
        $lng = $tmp[0];
        $lat = $tmp[1];
		
		// 验证子景区名存在
		$dbData = Db::fetch("select id from `pft_scenic` where scenic_name='$scenic_name' and apply_did=$apply_did limit 1");
		if(isset($dbData['id'])){
			if(($post['lastid'] && $post['lastid']!=$dbData['id']) || (!$post['lastid'])) 
				exit('{"status":"failure","msg":"景区名称已存在"}');
		}
		
		
		$set = "set apply_did=$apply_did,scenic_name='$scenic_name',tour_guide='$tour_guide',thumb_path='$thumb_path',intro_voice_path='$voice_path',degree_path='$degree_path',video_path='$video_path',addtime=now(),lng='$lng',lat='$lat',city='$city',province='$province',detail_addr='$daddress'";

		if(($id=$post['lastid'])){
			$sql = "update pft_scenic $set where id=$id limit 1";
		}else{
			$sql = "insert into pft_scenic $set";
		}
		
		$result = (Db::execute($sql)) ? 'success':'failure';

		// 如果有lid，则添加到后头
		if($parent_id){
			$insLast = Db::lastInsertId();
			$sql="select id,scenicid_list from uu_land where id=$lid limit 1";
			$GLOBALS['le']->query($sql);
			$GLOBALS['le']->fetch_assoc();
			$list_c = $GLOBALS['le']->f('scenicid_list');// 数组 array('景点ID'=>'推荐1，不推荐0')
			if($list_c){
				$arr = json_decode($list_c, true);
				$arr[$insLast] = 0;
				$upd = "update uu_land set scenicid_list='".json_encode($arr)."' where id=$lid and apply_did=$apply_did limit 1";
				$GLOBALS['le']->query($upd);
			}
		}
		
		echo '{"status":"'.$result.'","parent_id":"'.$parent_id.'","next":"'.$_REQUEST['nf'].'"}';
		break;
	// 更新线路关联信息
	case 'chScenic':
		$ids = $_REQUEST['status'];
		$lid = $_POST['lid'] + 0;
		$sql="update uu_land set scenicid_list='$ids' where id=$lid and apply_did='$apply_did' limit 1";
		if($GLOBALS['le']->query($sql)){
			if($_REQUEST['next']){
				echo '<script> window.parent.location.href="../product_publish.html?sid='.$lid.'";</script>';
			}else{
				echo '<script>alert("保存成功");</script>';
				echo '<script> window.parent.location.href="/new/d/prodlists.html";</script>';
			}
			
		}else{
			echo '<script>alert("保存失败，请重新保存");</script>';
			echo '<script>history.go(-1);</script>';
			exit;
		}
		// echo $sql;
		break;
	// 异步更新线路关联信息
	case 'sync_updScenic':
		$ids = $_REQUEST['sort'];
		$lid = $_POST['lid'] + 0;
		$sql="update uu_land set scenicid_list='$ids' where id=$lid and apply_did='$apply_did' limit 1";
		$state = ($GLOBALS['le']->query($sql)) ? 'success':'failure';
		exit('{"status":"'.$state.'"}');
		break;
	// 删除表的信息
	case 'delete':
		$id = $_REQUEST['id'] + 0;
		if($id<1) exit('{"code":102,"msg":"错误ID"}'); 
		$sql = "update pft_scenic set status=1 where id=$id and apply_did=$apply_did  limit 1";
		if(Db::execute($sql)) exit('{"code":100,"msg":"删除成功"}');
		exit('{"code":101,"msg":"删除失败，请重试"}');
		break;
	// 搜索子经典信息
	case 'search':
		include_once '/var/www/html/new/d/common/area.inc.php';
		$sql = "select * from pft_scenic where apply_did=$apply_did ";
		$keyword = $_REQUEST['k'];
		if($keyword!='') $sql.="and scenic_name like '%$keyword%'";
		$data = array();
		$dbData = Db::fetchAll($sql);
		foreach($dbData as $row){
			$row['province_cn'] = $areas[$row['province']];
			$row['city_cn'] = $areas[$row['city']];
			$data[]=$row;
		}
		$response['state'] = 'ok';
		$response['list'] = $data;
		echo json_encode($response);
		break;
	// 添加到页面后立即关联
	case 'updRelation':
		$ids = $_REQUEST['sort'];
		$lid = $_REQUEST['lid'] + 0;
		$sql="update uu_land set scenicid_list='$ids' where id=$lid and apply_did='$apply_did' limit 1";
		$state = $GLOBALS['le']->query($sql) ? 'success':'failure';
		exit('{"status":"'.$state.'"}');
		break;
	// 子景点关联下级景点
	case 'setChild':
		$id = $_REQUEST['parentId'] + 0;// 子景点ID
		// $childrenId = implode(',', $_REQUEST['childrenId']);
		$childrenId = $_REQUEST['childrenId'];
		if($id < 1) exit('{"status":"fail","msg":"错误ID"}'); 
		$sql = "update pft_scenic set child_ren='$childrenId' where id=$id and apply_did=$apply_did limit 1";
		if(Db::execute($sql)) exit('{"status":"success","msg":"更新成功"}');
		exit('{"status":"fail","msg":"更新失败，请重试"}');
		break;
	// 重点推荐更新
	case 'recommend':
		$status = $_REQUEST['status'] + 0;
		$lid = $_REQUEST['lid'] + 0;
		$sid = $_REQUEST['sid'] + 0;
		$sql = "select scenicid_list from uu_land where id=$lid and apply_did=$apply_did limit 1";
		$GLOBALS['le']->query($sql);
		$GLOBALS['le']->fetch_assoc();
		if(($str = $GLOBALS['le']->f('scenicid_list'))){
			$arr = json_decode($str, true);
			$arr[$sid] = $status;
			$state = (execute_update_land(json_encode($arr), $lid)) ? 'success':'failure';
			exit('{"status":"'.$state.'"}');
		}
		exit('{"status":"failure"}');
		break;
    default:
        break;
}

// 执行更改 uu_land 关联
function execute_update_land($list,$lid){
	$memberID = $_SESSION['sid'];
	$sql="update uu_land set scenicid_list='$list' where id=$lid and apply_did='$memberID' limit 1";
	return $GLOBALS['le']->query($sql);
}



function _addslashes($data){
	if(is_array($data)){
		foreach($data as $key=>$value){
			$data[$key] = _addslashes($value);
		}
		return $data;
	}
	return addslashes($data);
}